﻿
Public Class ResourceFactory
    Private _Engine As GameEngine
    Public ReadOnly Property Engine As GameEngine
        Get
            Return _Engine
        End Get
    End Property

    Protected Friend Sub New(withEngine As GameEngine)
        _Engine = withEngine
    End Sub

    Public ReadOnly SpriteFactory As SpriteFactory = New SpriteFactory(Me)

    Public Shared Sub SetPropertyValueFromString(info As System.Reflection.PropertyInfo, target As Object, valueString As String)
        Dim value As Object = Nothing
        Select Case info.PropertyType
            Case GetType(String)
                value = valueString
            Case GetType(Integer)
                Dim parsedValue As Integer
                If Integer.TryParse(valueString, parsedValue) Then
                    value = parsedValue
                End If
            Case GetType(UInteger)
                Dim parsedValue As UInteger
                If UInteger.TryParse(valueString, parsedValue) Then
                    value = parsedValue
                End If
            Case GetType(Long)
                Dim parsedValue As Long
                If Long.TryParse(valueString, parsedValue) Then
                    value = parsedValue
                End If
            Case GetType(ULong)
                Dim parsedValue As ULong
                If ULong.TryParse(valueString, parsedValue) Then
                    value = parsedValue
                End If
            Case GetType(Double)
                Dim parsedValue As Double
                If Double.TryParse(valueString, parsedValue) Then
                    value = parsedValue
                End If
            Case GetType(Single)
                Dim parsedValue As Single
                If Single.TryParse(valueString, parsedValue) Then
                    value = parsedValue
                End If
            Case GetType(Decimal)
                Dim parsedValue As Decimal
                If Decimal.TryParse(valueString, parsedValue) Then
                    value = parsedValue
                End If
            Case GetType(Boolean)
                Dim parsedValue As Boolean
                If Boolean.TryParse(valueString, parsedValue) Then
                    value = parsedValue
                End If
        End Select
        If value IsNot Nothing Then
            info.SetValue(target, value, Nothing)
        End If
    End Sub
End Class